#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#define Dispaly_P			1
#define Dispaly_Volume		2

/* widgets:
Layer_TV
Background_TV
Container_TV_Off
Text_TV_OFF
PopupButton_TV_ON
Container_TV_P_Off
Icon_TVP_JindutiaoOff
Container_TV_Volume_Off
Icon_TVVolume_JindutiaoOff
Container_TV_On
PopupButton_TVVolume_OFF
Container_TV_P_On
Container_TVP_Button
PopupButton_TVPEn
PopupButton_TVVolumeDis
Container_TVP_Text
Text_TVP_Pindao
Meter_TVP_Xuanzhuan
Icon_TVP_Pointer
CircleProgressBar_TVP
Text_TVP_PNum
Icon_TVP_Jingdu
Container_TV_Volume_On
Container_TVVolume_Button
PopupButton_TVPDis
PopupButton_TVVolumeEn
Container_TVVolume_Text
Text_TVVolume_YinLiang
Meter_TVVolume_Xuanzhuan
Icon_TVVolume_Pointer
CircleProgressBar_TVVolume
Text_TVVolume_VolumeNum
Icon_TVVolume_Jingdu
Button_Back_MainMenu
*/
static ITUBackground* Background_TV;
static ITUContainer* Container_TV_Off;
static ITUContainer* Container_TV_On;
static ITUMeter* Meter_TVVolume_Xuanzhuan;
static ITUMeter* Meter_TVP_Xuanzhuan;
static ITUText* Text_TVP_Pindao, *Text_TVVolume_YinLiang;
static uint8_t TV_Display_P_Or_Volume = Dispaly_P;
bool TVOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_TV)//首次加载
	{
		Background_TV = ituSceneFindWidget(&theScene, "Background_TV");
		assert(Background_TV);
		Container_TV_On = ituSceneFindWidget(&theScene, "Container_TV_On");
		assert(Container_TV_On);
		Container_TV_Off = ituSceneFindWidget(&theScene, "Container_TV_Off");
		assert(Container_TV_Off);
		Meter_TVVolume_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_TVVolume_Xuanzhuan");
		assert(Meter_TVVolume_Xuanzhuan);
		Meter_TVP_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_TVP_Xuanzhuan");
		assert(Meter_TVP_Xuanzhuan);
		Text_TVP_Pindao = ituSceneFindWidget(&theScene, "Text_TVP_Pindao");
		assert(Text_TVP_Pindao);
		Text_TVVolume_YinLiang = ituSceneFindWidget(&theScene, "Text_TVVolume_YinLiang");
		assert(Text_TVVolume_YinLiang);
	}
	//更具参数初始化界面
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.status == 1)//打开
	{
		ituWidgetSetVisible(Container_TV_Off, false);
		ituWidgetSetVisible(Container_TV_On, true);
	}
	else//关闭
	{
		ituWidgetSetVisible(Container_TV_On, false);
		ituWidgetSetVisible(Container_TV_Off, true);
	}
	ituMeterSetValue(Meter_TVVolume_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.Volume);
	ituMeterSetValue(Meter_TVP_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.P);

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_TVP_Pindao->fontIndex = 0;
		Text_TVP_Pindao->boldSize = 1;
		ituTextSetFontSize(Text_TVP_Pindao, 18);
		Text_TVVolume_YinLiang->fontIndex = 0;
		Text_TVVolume_YinLiang->boldSize = 1;
		ituTextSetFontSize(Text_TVVolume_YinLiang, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_TVP_Pindao->fontIndex = 8;
		Text_TVP_Pindao->boldSize = 0;
		ituTextSetFontSize(Text_TVP_Pindao, 16);
		Text_TVVolume_YinLiang->fontIndex = 8;
		Text_TVVolume_YinLiang->boldSize = 0;
		ituTextSetFontSize(Text_TVVolume_YinLiang, 16);
	}
	Set_Rotate_Block(30);//旋转步进
    return true;
}

bool TVOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool TVOnTimer(ITUWidget* widget, char* param)
{
	static int MeterVolumeValue = 0, MeterVolumeValue_L = 0;
	static int MeterPValue = 0, MeterPValue_L = 0;
	static uint32_t SendTime_V = Rotate_SendCmd_Time;
	static uint32_t SendTime_P = Rotate_SendCmd_Time;

	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备操作
	if (TV_Display_P_Or_Volume == Dispaly_P)//行程设置
	{
		MeterPValue = Meter_TVP_Xuanzhuan->value;
		if (Action_Cmd.User_Action == Clockwise_Rotation)//有用户操作
		{
			MeterPValue += Action_Cmd.Action_Times;
			if (MeterPValue > 100)MeterPValue = 100;
			ituMeterSetValue(Meter_TVP_Xuanzhuan, MeterPValue);//设置Meter百分比值
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation)
		{
			MeterPValue -= Action_Cmd.Action_Times;
			if (MeterPValue < 0)MeterPValue = 0;
			ituMeterSetValue(Meter_TVP_Xuanzhuan, MeterPValue);//设置Meter百分比值
			Clear_User_Action();
		}
	}
	else if (TV_Display_P_Or_Volume == Dispaly_Volume)//角度设置
	{
		MeterVolumeValue = Meter_TVVolume_Xuanzhuan->value;
		if (Action_Cmd.User_Action == Clockwise_Rotation)//有用户操作
		{
			MeterVolumeValue += Action_Cmd.Action_Times;
			if (MeterVolumeValue > 100)MeterVolumeValue = 100;
			ituMeterSetValue(Meter_TVVolume_Xuanzhuan, MeterVolumeValue);//设置Meter百分比值
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation)
		{
			MeterVolumeValue -= Action_Cmd.Action_Times;
			if (MeterVolumeValue < 0)MeterVolumeValue = 0;
			ituMeterSetValue(Meter_TVVolume_Xuanzhuan, MeterVolumeValue);//设置Meter百分比值
			Clear_User_Action();
		}
	}//发送执行报文
	if (MeterVolumeValue == MeterVolumeValue_L)
	{
		if (SendTime_V <= Rotate_SendCmd_Time)
			SendTime_V++;
		//停止操作0.6s后发送执行报文
		if (SendTime_V == Rotate_SendCmd_Time)
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	}
	else
	{
		SendTime_V = 0;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route = MeterVolumeValue;
		MeterVolumeValue_L = MeterVolumeValue;
	}
	if (MeterPValue == MeterPValue_L)
	{
		if (SendTime_P <= Rotate_SendCmd_Time)
			SendTime_P++;
		//停止操作0.6s后发送执行报文
		if (SendTime_P == Rotate_SendCmd_Time)
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
	}
	else
	{
		SendTime_P = 0;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.angle = MeterPValue;
		MeterPValue_L = MeterPValue;
	}
	//显示状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.status == 1)//打开
		{
			ituWidgetSetVisible(Container_TV_Off, false);
			ituWidgetSetVisible(Container_TV_On, true);

		}
		else//关闭
		{
			ituWidgetSetVisible(Container_TV_On, false);
			ituWidgetSetVisible(Container_TV_Off, true);
		}
		ituMeterSetValue(Meter_TVVolume_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.Volume);
		ituMeterSetValue(Meter_TVP_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.P);
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
    return true;
}

bool PopupButton_TV_ON_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.status = 1;
    return true;
}

bool PopupButton_TV_OFF_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_TV_Status.status = 0;
    return true;
}
bool PopupButton_TVVolume_Set(ITUWidget* widget, char* param)
{
	TV_Display_P_Or_Volume = Dispaly_Volume;
	return true;
}

bool PopupButton_TVP_Set(ITUWidget* widget, char* param)
{
	TV_Display_P_Or_Volume = Dispaly_P;
	return true;
}
